package se.perp.c4.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.BevelBorder;
import se.perp.c4.control.iface.ICardClickListener;
import se.perp.c4.model.Actor;
import se.perp.c4.model.Actor.Side;
import se.perp.c4.model.Card;
import se.perp.c4.model.Deck;

public class CardPickerDialog extends JDialog implements ICardClickListener {

    private Color dialogBGColor,  dialogTextColor;
    private final Deck myDeck;
    private final boolean useDiscardPile;
    private Card selectedCard = null;
    private final int dialogWidth = 500;
    private final int dialogHeight = 230;
    private List<Card> cardsToUse;
    private int  cardHeight;
    private Map<Card, CardImagePanel> cardImagePanels = new HashMap<Card, CardImagePanel>();
    private Actor opponent;
    private Deck opponentDeck;
    private JFrame owner;

    private class CardDisplayPanel extends JPanel {

        public CardDisplayPanel() {
            setBackground(ColorSet.BOARD_BG);
            setLayout(new GridLayout(1, cardsToUse.size(), 5, 0));
            for (Card c : cardsToUse) {
                CardImagePanel comp = cardImagePanels.get(c);
                add(comp);
                cardHeight = comp.getHeight();
            }
        }
    }

    private class TopPanel extends JPanel {

        private Font font;
        private String text = "~ Pick a card (double-click on it). ~";

        public TopPanel() {
            setPreferredSize(new Dimension(-1, 30));
            setBackground(dialogBGColor);
            font = FontSet.CARDPICKER_CAPTION;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2.setFont(font);
            g2.setColor(dialogTextColor);
            int strW = g2.getFontMetrics().stringWidth(text);
            int x = (getWidth() - strW) / 2;
            int y = g2.getFontMetrics().getHeight();
            g2.drawString(text, x, y);
        }
    }

    private class CenterPanel extends JPanel {

        public CenterPanel(Container owner) {

            CardDisplayPanel cardDisplay = new CardDisplayPanel();

            JScrollPane pane = new JScrollPane(cardDisplay,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            pane.setPreferredSize(new Dimension(dialogWidth - 20, cardHeight + 20));

            pane.getHorizontalScrollBar().setUnitIncrement(25);

            setBackground(dialogBGColor);
            setLayout(new FlowLayout());
            add(pane);
        }
    }

    public CardPickerDialog(JFrame owner, Deck myDeck, Deck opponentDeck, Actor opponent, boolean useDiscardPile) {
        super(owner);

        this.owner = owner;
        this.myDeck = myDeck;
        this.opponentDeck = opponentDeck;
        this.opponent = opponent;
        this.useDiscardPile = useDiscardPile;

        Side side;
        if (useDiscardPile) {
            side = myDeck.getDiscardPile().get(0).side;
        } else {
            side = myDeck.getDeck().get(0).side;
        }

        if (side == Side.DARK) {
            dialogBGColor = ColorSet.DARKCARD_BG;
        } else {
            dialogBGColor = ColorSet.LIGHTCARD_BG;
        }

        dialogTextColor = ColorSet.contrastingColor(dialogBGColor);

        if (useDiscardPile) {
            cardsToUse = myDeck.getDiscardPile();
        } else {
            cardsToUse = myDeck.getDeck();
        }

        if (owner != null) {
            Point px = owner.getLocationOnScreen();
            setLocation(px.x + (owner.getWidth() - dialogWidth) / 2,
                    px.y + (owner.getHeight() - dialogHeight) / 2);
        }

        JPanel mainPanel = new JPanel();

        for (Card c : cardsToUse) {
            CardImagePanel p = new CardImagePanel(c);
            p.setHoverSensitive(true);
            p.setClickListener(this);
            p.setBackground(ColorSet.BOARD_BG);
            cardImagePanels.put(c, p);
        }

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        setResizable(false);

        JFrame dummy = new JFrame();
        dummy.pack();
        int dummyHeight = dummy.getHeight();

        setSize(dialogWidth, dialogHeight + dummyHeight);
        CenterPanel centerPanel = new CenterPanel(this);

        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(new TopPanel(), BorderLayout.NORTH);
        mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        setContentPane(mainPanel);

        setModalityType(ModalityType.MODELESS);
    }

    public Card getSelectedCard() {
        return selectedCard;
    }

    public void cardPicked(Card c) {
        selectedCard = c;
        dispose();
    }
}